package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import com.leyou.item.service.impl.SpecificationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 规格
 */
@RestController
@RequestMapping("/spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 根据分类id查询规格组
     *
     * @param cid
     * @return
     */
    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupByCid(@PathVariable("cid") Long cid) {


        return ResponseEntity.ok(specificationService.queryGroupByCid(cid));

    }

    /**
     * 新增规格组
     *
     * @param specGroup
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroup specGroup) {

        System.out.println(specGroup);
        specificationService.saveGroup(specGroup);

        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /**
     * 修改规则组名
     *
     * @param specGroup
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroup specGroup) {
        specificationService.updateGroup(specGroup);

        return ResponseEntity.ok().build();
    }


    /**
     * 删除规格组
     *
     * @param id
     * @return
     */
    @DeleteMapping("/group/{id}")
    public ResponseEntity<Void> deleteGroup(@PathVariable Long id) {

        specificationService.deleteGroup(id);

        return ResponseEntity.ok().build();

    }

    /**
     * 可以更具规格组，分类查询规格参数
     *
     * @param gid 组id
     * @param cid 分类id
     * @param searching 是否搜索(给搜索团队用)
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> queryParamList(@RequestParam(value = "gid",required = false) Long gid,
                                                          @RequestParam(value = "cid",required = false) Long cid,
                                                          @RequestParam(value = "searching",required = false) Boolean searching) {

        List<SpecParam> specParams = specificationService.queryParamList(gid,cid,searching);
        return ResponseEntity.ok(specParams);

    }


    /**
     * 保存规格参数
     * @param specParam
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParam specParam) {

        specificationService.saveSpecParam(specParam);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新规格参数
     * @param specParam
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParam specParam){
        specificationService.updateSpecParam(specParam);
        return ResponseEntity.ok().build();

    }

    /**
     * 更具id删除规格参数
     * @return
     */
    @DeleteMapping("/param/{id}")
    public ResponseEntity<Void> deleteSpecParamById(@PathVariable("id") Long id){

        specificationService.deleteSpecParamById(id);

        return ResponseEntity.ok().build();
    }
    /*
    查询规格组和组的规格参数
     */
     @GetMapping("/group")
    ResponseEntity<List<SpecGroup>> queryListByCid(@RequestParam("cid") Long cid){


        return ResponseEntity.ok(specificationService.queryListByCid(cid));
     }


}
